package com.base.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.util.PageData;
import com.base.util.SessionUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by lee on 2016/6/23.
 */
public class ActionHandlerInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // TODO Auto-generated method stub

        boolean result = false;

        final String [] pathArray = new String[]{"/infoType","/hunyin","/salary_laowu","/laowu",
                "/chuguodoc","/chuguo","/zinvhunyin","/zinvyiju",
                "/zinvjiuye","/zinvzeren","/house","/gupiao","/jijin",
                "/qihuo","/qiye","/geti","/gotoCommit","/commit","/getHunYinSingleFlag",
                "/gotoExaminationApproval","/examineInfo","/login_index","/report",
                "/examineOKBtn","/examineNoBtn","/editSave","/singleInfoSave","/save","/updInfo","/delete"};

        String path = request.getServletPath();

        //静态资源不拦截,登录,登录界面
        if(path.startsWith("/media/")||path.equals("/sign_up_commit")||path.equals("/login_toLogin")||path.equals("/logout")){
            result = true;
        }else if(Arrays.asList(pathArray).contains(path)){
            PageData user = (PageData) SessionUtil.getHttpSession().getAttribute("USER");
            if(null != user){
                result = true;
            }else{
                response.sendRedirect(request.getContextPath() + "/login_toLogin");
                result = false;
            }
        }else{
            response.sendRedirect(request.getContextPath() + "/login_toLogin");
            result = false;
        }
        return result;
    }
}
